// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CLOUDAPPENGINE_CAEPIPEDATA_H
#define CLOUDAPPENGINE_CAEPIPEDATA_H

const uint32_t MAX_PIPE_SIZE = 1024 * 1024 * 20;
struct CaePipeData {
public:
    CaePipeData()
    {
        this->m_len = 0;
        this->m_buffer = nullptr;
        this->m_type = 0;
    }

    ~CaePipeData()
    {
        if (m_buffer != nullptr) {
            free(m_buffer);
            m_buffer = nullptr;
        }
    }

    uint32_t GetLen()
    {
        return this->m_len;
    }

    unsigned const char *GetBuffer() noexcept
    {
        return this->m_buffer;
    }

    uint8_t GetType()
    {
        return this->m_type;
    }

    bool Init(uint8_t *buf, uint32_t size, uint8_t type)
    {
        if ((size == 0) || (size > MAX_PIPE_SIZE)) {
            LOGE("Failed to Init buf, invalid size is %d.", size);
            if (size > 0) {
                free(buf);
            }
            this->m_buffer = nullptr;
            this->m_len = 0;
            return false;
        }
        this->m_buffer = buf;
        this->m_len = size;
        this->m_type = type;
        return true;
    }

private:
    uint32_t m_len;
    uint8_t *m_buffer;
    uint8_t m_type;
};

#endif // CLOUDAPPENGINE_CAEPIPEDATA_H
